/*
   Copyright 2011 Jason Drake (jadrake75@gmail.com)
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.javad.components;

import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;


@SuppressWarnings("serial")
public class ImageThumbnail extends JLabel {

	public static final int PREVIEW_SIZE = 80;
	public static final int IMAGE_PADDING = 4;
	private BufferedImage image;
	
	private int preview_size = PREVIEW_SIZE;
	
	public ImageThumbnail() {
		super();
	}
	
	public ImageThumbnail(Image image) {
		this(image, PREVIEW_SIZE);
	}
	
	public BufferedImage getImage() {
		return image;
	}
	
	public ImageThumbnail(Image image, int size) {
		this();
		setSize(size);
		setImage(image);
	}
	
	public void setSize(int size) {
		preview_size = size;
	}

	public void setImage(Image value) {
		if( value instanceof BufferedImage ) {
			image = (BufferedImage)value;
			setIcon( UIHelper.getScaledIcon(image, preview_size, IMAGE_PADDING));
			
		} else {
			image = null;
			setIcon( null );
		}
		repaint();
	}

}
